package com.ruoyi.project.system.interact.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.interact.mapper.DjGraphicDynamicMapper;
import com.ruoyi.project.system.interact.domain.DjGraphicDynamic;
import com.ruoyi.project.system.interact.service.IDjGraphicDynamicService;

/**
 * 绘声绘影动态Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-01
 */
@Service
public class DjGraphicDynamicServiceImpl implements IDjGraphicDynamicService 
{
    @Autowired
    private DjGraphicDynamicMapper djGraphicDynamicMapper;

    /**
     * 查询绘声绘影动态
     * 
     * @param id 绘声绘影动态主键
     * @return 绘声绘影动态
     */
    @Override
    public DjGraphicDynamic selectDjGraphicDynamicById(Long id)
    {
        return djGraphicDynamicMapper.selectDjGraphicDynamicById(id);
    }

    /**
     * 查询绘声绘影动态列表
     * 
     * @param djGraphicDynamic 绘声绘影动态
     * @return 绘声绘影动态
     */
    @Override
    public List<DjGraphicDynamic> selectDjGraphicDynamicList(DjGraphicDynamic djGraphicDynamic)
    {
        return djGraphicDynamicMapper.selectDjGraphicDynamicList(djGraphicDynamic);
    }

    /**
     * 新增绘声绘影动态
     * 
     * @param djGraphicDynamic 绘声绘影动态
     * @return 结果
     */
    @Override
    public int insertDjGraphicDynamic(DjGraphicDynamic djGraphicDynamic)
    {
        djGraphicDynamic.setCreateTime(DateUtils.getNowDate());
        djGraphicDynamic.setPublishTime(DateUtils.getNowDate());
        djGraphicDynamic.setAuditFlag("0"); // 默认待审核
        djGraphicDynamic.setViewCount(0L); // 默认浏览量为0
        djGraphicDynamic.setLikeCount(0L); // 默认点赞量为0
        djGraphicDynamic.setDelFlag("0"); // 默认未删除
        return djGraphicDynamicMapper.insertDjGraphicDynamic(djGraphicDynamic);
    }

    /**
     * 修改绘声绘影动态
     * 
     * @param djGraphicDynamic 绘声绘影动态
     * @return 结果
     */
    @Override
    public int updateDjGraphicDynamic(DjGraphicDynamic djGraphicDynamic)
    {
        djGraphicDynamic.setUpdateTime(DateUtils.getNowDate());
        return djGraphicDynamicMapper.updateDjGraphicDynamic(djGraphicDynamic);
    }

    /**
     * 批量删除绘声绘影动态
     * 
     * @param ids 需要删除的绘声绘影动态主键
     * @return 结果
     */
    @Override
    public int deleteDjGraphicDynamicByIds(Long[] ids)
    {
        return djGraphicDynamicMapper.deleteDjGraphicDynamicByIds(ids);
    }

    /**
     * 删除绘声绘影动态信息
     * 
     * @param id 绘声绘影动态主键
     * @return 结果
     */
    @Override
    public int deleteDjGraphicDynamicById(Long id)
    {
        return djGraphicDynamicMapper.deleteDjGraphicDynamicById(id);
    }

    /**
     * 审核动态
     * 
     * @param id 动态ID
     * @param auditFlag 审核标志（1-通过，2-拒绝）
     * @return 结果
     */
    @Override
    public int auditDynamic(Long id, Integer auditFlag)
    {
        DjGraphicDynamic dynamic = new DjGraphicDynamic();
        dynamic.setId(id);
        dynamic.setAuditFlag(String.valueOf(auditFlag));
        dynamic.setUpdateTime(DateUtils.getNowDate());
        return djGraphicDynamicMapper.updateDjGraphicDynamic(dynamic);
    }

    /**
     * 增加浏览量
     * 
     * @param id 动态ID
     * @return 结果
     */
    @Override
    public int incrementViewCount(Long id)
    {
        return djGraphicDynamicMapper.incrementViewCount(id);
    }

    /**
     * 查询已审核通过的公开动态
     * 
     * @return 动态列表
     */
    @Override
    public List<DjGraphicDynamic> selectPublicDynamics()
    {
        return djGraphicDynamicMapper.selectPublicDynamics();
    }

    /**
     * 根据用户权限查询动态列表
     * 管理员和拥有列表权限的用户可以看到所有动态
     * 普通用户只能看到审核通过的动态和公开的动态
     * 
     * @param djGraphicDynamic 查询条件
     * @return 动态列表
     */
    @Override
    public List<DjGraphicDynamic> selectDjGraphicDynamicListWithPermission(DjGraphicDynamic djGraphicDynamic)
    {
        User currentUser = ShiroUtils.getSysUser();
        
        // 如果是管理员或者拥有列表管理权限，可以查看所有动态
        if (User.isAdmin(currentUser.getUserId()) || 
            SecurityUtils.getSubject().isPermitted("hui:interact:graphic:list"))
        {
            return djGraphicDynamicMapper.selectDjGraphicDynamicList(djGraphicDynamic);
        }
        
        // 普通用户只能看到审核通过的动态和公开的动态
        return djGraphicDynamicMapper.selectDjGraphicDynamicListWithPermission(djGraphicDynamic);
    }

    /**
     * 根据用户权限查询单个动态详情
     * 
     * @param id 动态ID
     * @return 动态详情
     */
    @Override
    public DjGraphicDynamic selectDjGraphicDynamicByIdWithPermission(Long id)
    {
        User currentUser = ShiroUtils.getSysUser();
        DjGraphicDynamic dynamic = djGraphicDynamicMapper.selectDjGraphicDynamicById(id);
        
        if (dynamic == null) {
            return null;
        }
        
        // 如果是管理员或者拥有列表管理权限，可以查看所有动态
        if (User.isAdmin(currentUser.getUserId()) || 
            SecurityUtils.getSubject().isPermitted("hui:interact:graphic:list"))
        {
            return dynamic;
        }
        
        // 普通用户只能查看审核通过的动态或公开的动态
        if ("1".equals(dynamic.getAuditFlag()) || "1".equals(dynamic.getIsPublic()))
        {
            return dynamic;
        }
        
        return null; // 无权查看
    }
} 